#include "common.h"
#include <unordered_map>
namespace common {
bool isHappy(int n) {
    std::unordered_map<int, int> cache;
    int value = 0;
    int current_n;
    int num;
    while (value != 1 and cache.count(n) == 0) {
        current_n = n;
        while (n > 0) {
            num = n % 10;
            n = n / 10;
            value += num * num;
        }
        if (value == 1)
            return true;
        else {
            n = value;
            if (cache.count(current_n) == 0) {
                cache[current_n] = value;
                value = 0;
            }
        }
    }
    return false;
}
} // namespace common
